﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace Giraffe.Client.Common.WindowsController
{
    public class WindowSettings : IWindowSettings
    {
        private readonly Window _window;

        public WindowSettings(Window window)
        {
            _window = window;
        }

        public double Height
        {
            get
            {
                return _window.Height;
            }
            set
            {
                if (!Application.Current.Dispatcher.CheckAccess())
                {
                    Application.Current.Dispatcher.Invoke((Action) (() => Height = value));
                    return;
                }
                _window.Height = value;
            }
        }

        public double Width
        {
            get
            {
                return _window.Width;
            }
            set
            {
                if (!Application.Current.Dispatcher.CheckAccess())
                {
                    Application.Current.Dispatcher.Invoke((Action)(() => Width = value));
                    return;
                }
                _window.Width = value;
            }
        }

        public string Title
        {
            get
            {
                return _window.Title;
            }
            set
            {
                if (!Application.Current.Dispatcher.CheckAccess())
                {
                    Application.Current.Dispatcher.Invoke((Action)(() => Title = value));
                    return;
                }
                _window.Title = value;
            }
        }

        public bool ShowInTaskbar
        {
            get
            {
                return _window.ShowInTaskbar;
            }
            set
            {
                _window.ShowInTaskbar = value;
            }
        }

        public ResizeMode ResizeMode
        {
            get { return _window.ResizeMode;}
            set { _window.ResizeMode = value; }
        }

        public ImageSource Icon
        {
            get { return _window.Icon; }
            set { _window.Icon = value; }
        }

        public WindowStartupLocation WindowStartupLocation
        {
            get { return _window.WindowStartupLocation; }
            set { _window.WindowStartupLocation = value; }
        }

        public WindowState WindowState
        {
            get { return _window.WindowState; }
            set { _window.WindowState = value; }
        }

        public double FontSize
        {
            get { return _window.FontSize; }
            set { _window.FontSize = value; }
        }

        public SizeToContent SizeToContent
        {
            get { return _window.SizeToContent; }
            set { _window.SizeToContent = value; }
        }

        public void AddRemoteEventHandler(RoutedEvent @event, Delegate handler)
        {
            _window.AddHandler(@event, handler);
        }

        public void RemoveRemoteEventHandler(RoutedEvent @event, Delegate handler)
        {
            _window.RemoveHandler(@event, handler);
        }

        public void AddHandler(string eventName, Delegate handler)
        {
            var eventInfo = typeof(Window).GetEvent(eventName, BindingFlags.Public | BindingFlags.Instance);
            if (eventInfo == null) throw new ArgumentException("eventName");

            eventInfo.RemoveEventHandler(_window, handler);
            eventInfo.AddEventHandler(_window, handler);
        }

        public void RemoveHandler(string eventName, Delegate handler)
        {
            var eventInfo = typeof(Window).GetEvent(eventName, BindingFlags.Public | BindingFlags.Instance);
            if (eventInfo == null) throw new ArgumentException("eventName");

            eventInfo.RemoveEventHandler(_window, handler);
        }
    }
}